#!/usr/bin/env bash

DIVIDING_LINE="-----------------------------------------------------------------------------"

################################################################################################################################################################
#			Extract strings from APP-MANAGER and modules
################################################################################################################################################################

[ -f ./APP-MANAGER ] && AMVERSION=$(grep "^AMVERSION=" APP-MANAGER | tr '"' '\n' | grep "^[0-9]") || AMVERSION=$(am -v)

mkdir -p translations && rm -f translations/source.*

cat <<-HEREDOC >> translations/source.pot
# File with translation for AM
# FIRST AUTHOR <EMAIL@ADDRESS>, $(date +"%Y")
msgid ""
msgstr ""
"Project-Id-Version: AM ${AMVERSION}\n"
"Report-Msgid-Bugs-To: https://github.com/ivan-hc/AM\n"
"POT-Creation-Date: $(date +"%Y-%m-%d")\n"
"PO-Revision-Date: $(date +"%Y-%m-%d")\n"
"Last-Translator: John Doe <john.doe@example.com>\n"
"Language-Team: English <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
HEREDOC

items="APP-MANAGER modules/database.am modules/install.am modules/management.am modules/sandboxes.am modules/template.am"
if [ ! -f ./APP-MANAGER ]; then
	for item in $items; do
		bash --dump-po-strings "/opt/am/$item" >> translations/source.pot
	done
else
	for item in $items; do
		bash --dump-po-strings "$item" >> translations/source.pot
	done
fi

msguniq translations/source.pot -o translations/source.po

msgcat --output-file=translations/source.pot --unique --indent --no-wrap translations/source.po

sed -i 's# /opt/am/# am/#g' translations/source*

if [ -s translations/source.po ] && [ -s translations/source.pot ]; then
	printf "%b\nSources are updated! \n%b\n" "$DIVIDING_LINE" "$DIVIDING_LINE"
else
	printf "%b\nSomething went wrong, a source file is empty, exiting... \n%b\n" "$DIVIDING_LINE" "$DIVIDING_LINE" && exit 1
fi


################################################################################################################################################################
#			Update translations and locale files
################################################################################################################################################################

[ -d ./translations ] && po_files=$(find translations/po-files/* -name "*.po" | tr ' ' '\n' | grep ".po$" | grep -v "source" | xargs)

[ -z "$po_files" ] && echo "Something went wrong, exiting..." && exit 1

for l in $po_files; do
	locale=""
	locale=$(echo "$l"  | sed 's:.*/::; s/.po$//g')
	printf "%b\n%b\n%b\n" "$DIVIDING_LINE" "$locale" "$DIVIDING_LINE"
	[ -z "$locale" ] && echo "Something went wrong, exiting..." && exit 1
	mkdir -p translations/usr/share/locale/"$locale"/LC_MESSAGES
	msgmerge -U "$l" translations/source.pot
	if [ ! -s "$l" ]; then
		echo "WARNING, $l is empty, exiting..." && exit 1
	else
		msgfmt -o translations/usr/share/locale/"$locale"/LC_MESSAGES/am.mo "$l"
	fi
done

rm -f translations/po-files/*~

printf "%b\nAll localizationfiles are updated\n%b\n" "$DIVIDING_LINE" "$DIVIDING_LINE"
